package com.css.msgcenter.admin.msgapp.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.db.page.Page;
import com.css.msgcenter.server.QueryMsg;
import com.css.msgcenter.server.model.MsgApp;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

public class DirMsgApp extends AdminAction{
	private static Log log = LogFactory.getLog(DirMsgApp.class);
	private String appName;
	private String appCode;
	private String openFlag;
	private Page page;
	
	public DirMsgApp(){
		page = new Page();
		page.setCountField("a.uuid");
	}
	
	@Override
	protected String adminGo() {
		try{
			QueryMsg qm = new QueryMsg("select a.uuid from MsgApp a " + getWhere() + getOrder());
			setWhere(qm);
			page = qm.page(page);
			page.setResults(QueryMsg.idToObj(MsgApp.class,page.getResults()));
			return Action.SUCCESS;
		}catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			setMessage(Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}
	
	public String getWhere() {
		StringBuffer sb = new StringBuffer(" where 1=1 ");
		if(StringHelper.isNotEmpty(openFlag))
			sb.append(" and a.openFlag = :openFlag");
		if(StringHelper.isNotEmpty(appCode))
			sb.append(" and a.appCode like :appCode");
		if(StringHelper.isNotEmpty(appName))
			sb.append(" and a.appName like :appName");
 		return sb.toString();
	}
	public void setWhere(QueryMsg qm) {
		if (StringHelper.isNotEmpty(openFlag))
			qm.setParameter("openFlag", openFlag);
		if(StringHelper.isNotEmpty(appCode))
			qm.setParameter("appCode", "%" + appCode.trim() + "%");
		if(StringHelper.isNotEmpty(appName))
			qm.setParameter("appName", "%" + appName.trim() + "%");
	}
	public String getOrder() {
		return StringHelper.isNotEmpty(page.getOrderByString()) ? page.getOrderByString() : " order by a.createdTime desc ";
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public String getAppCode() {
		return appCode;
	}

	public void setAppCode(String appCode) {
		this.appCode = appCode;
	}

	public String getOpenFlag() {
		return openFlag;
	}

	public void setOpenFlag(String openFlag) {
		this.openFlag = openFlag;
	}


}
